local que_qingkong = fk.CreateSkill {
    name = "que_qingkong",
    dynamic_desc = function(self, player)
        if player:getMark("que_qingkong_gai") == 0 then
            return "que_qingkong_canuse:" .. player:getMark("que_qingkong")
        elseif player:getMark("que_qingkong_gai") > 0 and player:getMark("que_qingkong") == 0 then
            return "que_qingkongex1"
        elseif player:getMark("que_qingkong_gai") > 0 and player:getMark("que_qingkong") > 0 then
            return "que_qingkongex1_canuse:" .. player:getMark("que_qingkong")
        end
    end,
}
Fk:loadTranslationTable {
    ["que_qingkong"] = "青空",
    [":que_qingkong"] = "出牌阶段限零次，你可以弃置一张非锦囊牌，视为使用一张〖鸾歌〗已获得过的牌，并从〖鸾歌〗中移除此牌(〖鸾歌〗每获得三张牌，此技能使用次数+1)。",

    [":que_qingkong_canuse"] = "出牌阶段限{1}次，你可以弃置一张非锦囊牌，视为使用一张〖鸾歌〗已获得过的牌，并从〖鸾歌〗中移除此牌(〖鸾歌〗每获得三张牌，此技能使用次数+1)。",

    [":que_qingkongex1"] = "出牌阶段限零次，你可以视为使用一张本回合未使用且结算过的普通锦囊牌(当你受到伤害后，此技能使用次数+1)。",

    [":que_qingkongex1_canuse"] = "出牌阶段限{1}次，你可以视为使用一张本回合未使用且结算过的普通锦囊牌(当你受到伤害后，此技能使用次数+1)。",
}
que_qingkong:addEffect("viewas", {
    prompt = "青空：请选择一张视为使用的牌，并弃置一张非锦囊牌",
    pattern = ".|.|.|.|.|trick",
    times = function(self, player)
        return 0 + player:getMark("que_qingkong")
    end,
    interaction = function(self, player)
        if player:getMark("que_qingkong_gai") == 0 then
            local all_names = {}
            for _, i in ipairs(player:getTableMark("@$que_luange_cards")) do
                if Fk:cloneCard(i).sub_type ~= Card.SubtypeDelayedTrick then
                    table.insertIfNeed(all_names, i)
                end
            end
            local names = player:getViewAsCardNames(que_qingkong.name, all_names)
            return UI.CardNameBox { choices = names, all_choices = all_names }
        else
            local mark = player:getTableMark("que_qingkong_usedcard-turn")
            local all_names = table.filter(Fk:getAllCardNames("t"), function(name)
                local card = Fk:cloneCard(name)
                return not table.contains(mark, card.trueName)
            end)
            local names = player:getViewAsCardNames(que_qingkong.name, all_names)
            if #names == 0 then return end
            return UI.CardNameBox { choices = names, all_choices = all_names }
        end
    end,
    card_filter = function(self, player, to_select, selected)
        if player:getMark("que_qingkong_gai") == 0 then
            return #selected == 0 and not player:prohibitDiscard(to_select) and
                Fk:getCardById(to_select).type ~= Card.TypeTrick
        else
            return false
        end
    end,
    view_as = function(self, player, cards)
        if player:getMark("que_qingkong_gai") == 0 then
            if #cards ~= 1 then return end
            local card = Fk:cloneCard(self.interaction.data)
            card.skillName = que_qingkong.name
            card:addFakeSubcards(cards)
            return card
        else
            local card = Fk:cloneCard(self.interaction.data)
            card.skillName = que_qingkong.name
            return card
        end
    end,
    before_use = function(self, player, use)
        local room = player.room
        if player:getMark("que_qingkong_gai") == 0 then
            room:removePlayerMark(player, "que_qingkong", 1)
            room:throwCard(use.card.fake_subcards, "que_qingkong", player, player)
        else
            room:removePlayerMark(player, "que_qingkong", 1)
        end
    end,
    after_use = function(self, player, use)
        local room = player.room
        if player:getMark("que_qingkong_gai") == 0 then
            room:removeTableMark(player, "@$que_luange_cards", self.interaction.data)
        end
    end,
    enabled_at_play = function(self, player)
        if player:getMark("que_qingkong_gai") == 0 then
            return player:getMark("que_qingkong") > 0 and #player:getTableMark("@$que_luange_cards") > 0
        else
            return player:getMark("que_qingkong") > 0
        end
    end,
    enabled_at_response = function(self, player, response)
        if player:getMark("que_qingkong_gai") == 0 then
            return player:getMark("que_qingkong") > 0 and #player:getTableMark("@$que_luange_cards") > 0
        else
            return player:getMark("que_qingkong") > 0
        end
    end,
})
que_qingkong:addEffect(fk.Damaged, {
    can_refresh = function(self, event, target, player, data)
        return target == player and player:hasSkill(que_qingkong.name) and player:getMark("que_qingkong_gai") > 0
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        room:addPlayerMark(player, "que_qingkong")
    end
})
que_qingkong:addEffect(fk.CardUseFinished, {
    anim_type = "support",
    can_refresh = function(self, event, target, player, data)
        local mark = player:getTableMark("que_qingkong_usedcard-turn")
        return target == player and player:hasSkill(que_qingkong.name) and data.card.type == Card.TypeTrick and
            not table.contains(mark, data.card.trueName) and player:getMark("que_qingkong_gai") > 0
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        room:addTableMarkIfNeed(player, "que_qingkong_usedcard-turn", data.card.trueName)
    end,
})
return que_qingkong
